<template>
  <!--// v-if 是 vue 的一个指令-->
  <!--// $route.path 是当前路由对象的一个路径，会被解析为绝对路径-->
  <!--// $route.path !== '/time-entries/log-time' 为true是显示，为false是不显示-->
  <!--// to 路由跳转地址-->
  <div>
    <router-link
      v-if="$route.path !== '/time-entries/log-time'"
      to="/time-entries/log-time"
      class="btn btn-primary">
      创建
    </router-link>

    <div v-if="$route.path === '/time-entries/log-time'">
      <h3>创建</h3>
    </div>

    <hr>

    <router-view></router-view>

    <div class="time-entries">
      <p v-if="!plans.length">还没有任何计划</p>

      <div class="list-group">

        <!-- v-for循环 注意参数顺序(item.index) in items -->

        <a class="list-group-item" v-for="(plan,index) in plans">

          <div class="row">
            <div class="col-sm-2 user-details">

              <!-- :src 属性，这个是vue的属性绑定简写，v-bind 可以缩写为 :
              比如 a标签的 href，可以简写为 :href
              并且在 vue的指令里就一定不要写插值表达式了，(:src={{XXX}}}),vue会自动解析
              -->

              <img :src="plan.avatar" class="avatar img-circle img-responsive"/>

              <p class="text-center">
                <strong>
                  {{ plan.name }}
                </strong>
              </p>
            </div>

            <div class="col-sm-2 text-center time-block">
              <h3 class="list-group-item-text total-time">
                <i class="glyphicon glyphicon-calendar"></i>
                {{ plan.totalTime }} 小时
              </h3>

              <p class="label label-primary text-center">
                <i class="glyphicon glyphicon-calendar"></i>
                {{ plan.date }}
              </p>
            </div>

            <div class="col-sm-7 comment-section">
              <p class="text-info">{{ plan.comment }}</p>
            </div>

            <div class="col-sm-1">
              <button class="btn btn-xs btn-danger delete-button" @click="deletePlan(index)">
                X
              </button>
            </div>

          </div>
        </a>
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    name: "TimeEntries",
    computed: {
      plans() {
        // 从Store 中取出数据
        return this.$store.state.list;
      }
    },
    methods: {
      deletePlan(idx) {
        // 减去总时间
        this.$store.dispatch('decTotalTime', this.plans[idx].totalTime);

        // 删除该计划
        this.$store.dispatch('deletePlan', idx);
      }
    }
  }
</script>

<style>
  .avatar {
    height: 75px;
    margin: 0 auto;
    margin-top: 10px;
    margin-bottom: 10px;
  }

  .user-details {
    background: #f5f5f5;
    border-right: 1px solid #ddd;
    margin: -10px 0;
  }

  .time-block {
    padding: 10px;
  }

  .comment-section {
    padding: 20px;
  }

</style>
